const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    tabindex:0,
    order: [],
    page:1,
    pageSize:10,
    isEnd:false,
    isloading:true,
  },
  onShow: function (option) {
    this.orderlist()
  },
  onShow(){
    this.setData({
      order: [],
      page:1,
      pageSize:10,
      isEnd:false,
      isloading:true
    })
    this.orderlist()
  },
  onPullDownRefresh(){
    this.setData({
      order: [],
      page:1,
      pageSize:10,
      isEnd:false,
      isloading:true
    })
    this.orderlist()
    wx.stopPullDownRefresh()
  },
  //实现复制
  handleCopy(e){
    console.log('e==>',e)
    let number=e.target.dataset.number

     
    if(!number){
      return
    }
    
    wx.setClipboardData({
      data:number,
      success:function (res) {
        wx.getClipboardData({
          success:function (res) {
          wx.showToast({
            title: '复制成功',
          })
             
          }
 
        })
 
      }
 
    })
  },

  tab(e){
    var index=e.currentTarget.dataset.index;
    this.setData({
      tabindex:index,
      order: [],
      page:1,
      pageSize:10,
      isEnd:false,
      isloading:true
    })
    this.orderlist()
  },
  orderlist(){
    if(this.data.tabindex!=4){
      var data={
        orderStatus:this.data.tabindex==0?99:this.data.tabindex==1?0:this.data.tabindex==2?3:this.data.tabindex==3?5:'',
        page:this.data.page,
        pageSize:this.data.pageSize
      }
      Controller.orderlist(data).then(res=>{
        if(res.data.baseData && res.data.baseData.list.length>0){
          this.setData({
            order:this.data.order.concat(res.data.baseData.list),
          })
          
          if(res.data.baseData.lastPage){
            this.setData({
              isEnd:res.data.baseData.lastPage
            })
          }
        }else{
          this.setData({
            isEnd:true
          })
        }
        this.setData({
          isloading:false
        })
      })
    }else{
      var data={
        page:this.data.page,
        pageSize:this.data.pageSize
      }
      Controller.refundlist(data).then(res=>{
        if(res.data.baseData && res.data.baseData.list.length>0){
             
          let list=res.data.baseData.list

          list=list.map(v=>v.visible=false)

          this.setData({
            order:this.data.order.concat(list),
          })
          
          if(res.data.baseData.lastPage){
            this.setData({
              isEnd:res.data.baseData.lastPage
            })
          }
        }else{
          this.setData({
            isEnd:true
          })
        }
        this.setData({
          isloading:false
        })
      })
    }
  },
  onReachBottom(){
    if(this.data.isEnd){
      return
    }else{
      this.setData({
        page:this.data.page+1
      })
      this.orderlist()
    }
    
  },
  
  godetail(e){
    if(e.currentTarget.dataset.index!=4){
      wx.navigateTo({
        url: '/pages/Me/orderdetail?id='+e.currentTarget.dataset.id
      })
    }else{
      wx.navigateTo({
        url: '/pages/service/result?id='+e.currentTarget.dataset.id
      })
    }
  },
  rebuy(e){
    wx.navigateTo({
      url: '/pages/mall/mall'
    })
  },
  cancelpay(e){
    var id=e.currentTarget.dataset.id;
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否取消支付?',
      success(res) {
        if (res.confirm) {
          var data={
            orderNumber:id
          }
          Controller.ordercancel(data).then(res=>{
            wx.showToast({
              title:"取消成功"
            })
            that.setData({
              order: [],
              page:1,
              pageSize:10,
              isEnd:false,
            })
            that.orderlist()
          })
        }
      }
    })
  },
  getmore(e){
   let id=e.currentTarget.dataset.id

   console.log('getmore id',id)

   let list=this.data.order

   for(let item of list){
    
    if(item.orderNumber===id){
        
      item.visible=!item.visible
      
      break;

    }

   }
   this.setData({
     order:list
   })

   
  },
  delete(e){
    var id=e.currentTarget.dataset.id;
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否删除订单?',
      success(res) {
        if (res.confirm) {
          var data={
            orderNumber:id
          }
          Controller.orderdelete(data).then(res=>{
            wx.showToast({
              title:"删除成功"
            })
            that.setData({
              order: [],
              page:1,
              pageSize:10,
              isEnd:false,
            })
            that.orderlist()
          })
        }
      }
    })
  },
  cancelafter(e){
    var id=e.currentTarget.dataset.id;
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否取消退款?',
      success(res) {
        if (res.confirm) {
          var data={
            orderNumber:id
          }
          Controller.refundcancel(data).then(res=>{
            wx.showToast({
              title:"取消成功"
            })
            that.setData({
              order: [],
              page:1,
              pageSize:10,
              isEnd:false,
            })
            that.orderlist()
          })
        }
      }
    })
  },
  pay(e){
    var id=e.currentTarget.dataset.id;
    var price=e.currentTarget.dataset.price;
    var item=e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/mall/pay?id='+ id +'&price='+price+'&paytype='+item.payType
    })
    // var that=this;
    // Controller.pay({ orderNumber: that.data.no, payType:1}).then(res => {
    //   let data=res.data.baseData;
    //   wx.requestPayment({
    //     nonceStr: data.nonceStr,
    //     package: data.package,
    //     paySign: data.paySign,
    //     timeStamp: data.timeStamp,
    //     signType: data.signType,
    //     success(res){
    //       setTimeout(() => {
    //         wx.redirectTo({
    //           url: "/pages/Me/order"
    //         })
    //       },1000)
    //       wx.showToast({
    //         title: "支付成功",
    //         icon: 'none',
    //         duration: 1000
    //       });
    //     },
    //     fail(res){
    //       setTimeout(() => {
    //         wx.redirectTo({
    //           url: "/pages/Me/order"
    //         })
    //       },1000)
    //       wx.showToast({
    //         title: "支付失败",
    //         icon: 'none',
    //         duration: 1000
    //       });
    //     }
    //   })
    // }).catch(res=>{

    // })
  },
})